package com.artqiyi.dollmachine.views.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.andview.refreshview.XRefreshView;
import com.artqiyi.dollmachine.R;
import com.artqiyi.dollmachine.common.Base.BaseActivity;
import com.artqiyi.dollmachine.common.constant.Constant;
import com.artqiyi.dollmachine.model.entity.GameRecordBean;
import com.artqiyi.dollmachine.presenter.MineCatchRecordPresenter;
import com.artqiyi.dollmachine.presenter.base.PresenterCallbackCollection;
import com.artqiyi.dollmachine.utils.RxRecyclerViewDividerTool;
import com.artqiyi.dollmachine.utils.ScreenUtil;
import com.artqiyi.dollmachine.views.adapter.MineCatchRecordAdapter;
import com.artqiyi.dollmachine.views.widget.CommonFooter;
import com.artqiyi.dollmachine.views.widget.CommonHeader;
import com.artqiyi.dollmachine.views.widget.CustomGifHeader;
import com.artqiyi.dollmachine.views.widget.MultipleStatusView;
import com.artqiyi.dollmachine.views.widget.ToolBarView;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * author: PengCheng
 * time: 2017/12/8 0008
 * desc: 我的抓取记录
 */

public class MineCatchRecordActivity extends BaseActivity<MineCatchRecordPresenter> implements PresenterCallbackCollection.MineCatchRecordActivity {
    @Bind(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.xRefreshView)
    XRefreshView mXRefreshView;
    @Bind(R.id.layoutEmptyView)
    LinearLayout layoutEmptyView;

    private MineCatchRecordAdapter mAdapter;

    private List<GameRecordBean> mDollBeans = new ArrayList<>();

    private int pageNum = 1;

    @Override
    public MineCatchRecordPresenter getPresenter() {
        return new MineCatchRecordPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_mine_catch_record;
    }

    @Override
    protected void handleToolBar(ToolBarView toolBar) {
        super.handleToolBar(toolBar);
        toolBar.setTitleText("抓取记录");
        toolBar.setBackPressed(this);
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        mAdapter = new MineCatchRecordAdapter(mDollBeans);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(MineCatchRecordActivity.this));
        mRecyclerView.addItemDecoration(new RxRecyclerViewDividerTool(ScreenUtil.dp2px(MineCatchRecordActivity.this, 5f)));
        mRecyclerView.setAdapter(mAdapter);
        CommonHeader header = new CommonHeader(MineCatchRecordActivity.this);
        header.setBackground(R.color.white);
        CommonFooter footer = new CommonFooter(MineCatchRecordActivity.this);
        footer.setBackground(R.color.white);
        mXRefreshView.setCustomHeaderView(header);
        mXRefreshView.setCustomFooterView(footer);
        mXRefreshView.setAutoLoadMore(true);
        mXRefreshView.setPinnedTime(1000);
        mXRefreshView.setMoveForHorizontal(true);
        mXRefreshView.setPullLoadEnable(true);
        mXRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {

            @Override
            public void onRefresh(boolean isPullDown) {
                mXRefreshView.setLoadComplete(false);
                pageNum = 1;
                mPresenter.getMineCatchRecord(pageNum);
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                mPresenter.getMineCatchRecord(pageNum);
            }
        });
    }

    @Override
    public void reLoadData() {
        super.reLoadData();
        pageNum = 1;
        mPresenter.getMineCatchRecord(pageNum);
    }

    @Override
    public void onGetMineCatchRecordSuccess(List<GameRecordBean> beans) {
        if (null != mXRefreshView) {
            mXRefreshView.stopRefresh();
            mXRefreshView.stopLoadMore();
        }

        if (null != beans && beans.size() != 0) {
            showEmptyView(false);
            if (pageNum == 1) {
                mDollBeans.clear();
            }
            mDollBeans.addAll(beans);
            mAdapter.notifyDataSetChanged();
            if (beans.size() < 20) {
                mXRefreshView.setLoadComplete(true);
            }
            pageNum++;
        } else {
            showEmptyView(true);
        }
    }

    @Override
    public void onGetMineCatchRecordFail(String msg) {
        if (null != mXRefreshView) {
            mXRefreshView.stopRefresh();
            mXRefreshView.setLoadComplete(true);
        }
        showEmptyView(true);
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    private void showEmptyView(boolean isShow) {
        layoutEmptyView.setVisibility(isShow ? View.VISIBLE : View.GONE);
        mXRefreshView.setVisibility(isShow ? View.GONE : View.VISIBLE);
    }

}
